<?php

namespace app\scm\controller;

use app\admin\common\Base;
use app\admin\common\Excel;
use think\Db;
use app\admin\model\Store as StoreModel;
//智能补货
class Autoremedy extends Base
{
    public function index()
    {
        $data = $this->request->param();
        $post=$data;

        if (isset($data['ajax']) && $data['ajax'] == 100) {

            $map = '';
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);
            // if (!isset($data['search']) || $data['search'] != 1) {
            //     $list=[];
            //     $count=0;
            //    return $this->grid_data($list, $count, $page, $limit);
            // }
            $search=[];
            $search1=[];//防止覆盖

            $where=[];//

            if(isset($data['goods_id'])&&$data['goods_id']!=''){
                $search['id']=['in',$data['goods_id']];    
            }
            if (!isset($data['goods_category_id']) || $data['goods_category_id'] == 0 || $data['goods_category_id'] == '') {
            } else {
                $c = Db::name('goods')->where('goods_category_id', $data['goods_category_id'])->select();
                $c['ids'] = implode(',', array_column($c, 'id'));       
                $search1['id']= ['in', $c['ids']];
            }
            if(isset($data['store_id'])&&$data['store_id']!=''){
                $where['store_id']= ['in',$data['store_id']];
            }
            $id=intval($data['goods_id']);
            //halt($id);
           // halt($search);
            $list = Db::name('goods')->where($search)->where($search1)->where('is_del',0)->where('enterprise_id',$this->EnterpriseId)->select();
            //o_type 0入库1出库
            foreach ($list as $key => &$val) {
                $list[$key]['good_id']=$val['id'];
                $list[$key]['cate_name']= Db::name('goods_category')->where('id', $val['goods_category_id'])->value('name');
                $list[$key]['unit_name']= Db::name('unit')->where('id', $val['unit'])->value('name');
                //$list[$key]['sales_num']=Db::name('stock_record_good')->alias('a')->join('stock_record b', 'a.pid = b.id')->field('a.*,b.*')->where('b.status',0)->where('b.is_del', 0)->where('b.enterprise_id', $this->EnterpriseId)->where('a.o_type',1)->where('a.o_way',1)->where('good_id',$val['id'])->where($where)->sum('number');//销售在定量
                $list[$key]['sales_num']=Db::name('order_good')->alias('a')->join('order b', 'a.order_id = b.id')->field('a.*,b.*')->where('b.is_del', 0)->where('b.enterprise_id', $this->EnterpriseId)->where('b.o_type',0)->where('good_id',$val['id'])->where($where)->sum('shuliang');//销售在定量
                $list[$key]['online_sales_num']=0;//网店订购量
                $list[$key]['cg_num']=Db::name('order_good')->alias('a')->join('order b', 'a.order_id = b.id')->field('a.*,b.*')->where('b.is_del', 0)->where('b.enterprise_id', $this->EnterpriseId)->where('b.o_type',3)->where('good_id',$val['id'])->where($where)->sum('shuliang');//采购在订量
                //$list[$key]['cg_num']=Db::name('stock_record_good')->alias('a')->join('stock_record b', 'a.pid = b.id')->field('a.*,b.*')->where('b.status',0)->where('b.is_del', 0)->where('b.enterprise_id', $this->EnterpriseId)->where('a.o_type',0)->where('a.o_way',0)->where('good_id',$val['id'])->where($where)->sum('number');//采购在订量

                $j=Db::name('stock_record_good')->alias('a')->join('stock_record b', 'a.pid = b.id')->field('a.*,b.*')->where('b.status',1)->where('b.is_del', 0)->where('b.enterprise_id', $this->EnterpriseId)->where('a.o_type',0)->where('good_id',$val['id'])->where($where)->sum('number');//进

                $c=Db::name('stock_record_good')->alias('a')->join('stock_record b', 'a.pid = b.id')->field('a.*,b.*')->where('b.status',1)->where('b.is_del', 0)->where('b.enterprise_id', $this->EnterpriseId)->where('a.o_type',1)->where('good_id',$val['id'])->where($where)->sum('number');//出

                $list[$key]['ck']=$j-$c;//库存余额 
                // if($val['id']==7){
                //     halt($j);
                // }

                $list[$key]['low_num']=Db::name('goods_stock_warning')->where('is_del',0)->where('enterprise_id',$this->EnterpriseId)->where('goods_id',$val['id'])->where($where)->sum('low_stock');//最低库存 
                $jy_num= $val['sales_num']+$val['online_sales_num']-$val['ck']- $val['cg_num']+$val['low_num'];
                $list[$key]['jy_num']= $jy_num;//建建议采购量 = 销售在订量 + 网店订购量 - 库存余额- 采购在订量 + 最低库存
                $val['del']=0;   
                if(isset($data['jycgl'])&&$data['jycgl']==1){
                    if ($jy_num<=0) {
                        $val['del']=1;   
                    } 
                } 
            }

            $list1=[];
            //halt($list);
            $i="";
            if (count($list)>0) {
                foreach ($list as $ke => $va) {
                    $i.=$va['id']."aa";
                    //halt($va);
                    if($va['del']==0){
                        $i.=$va['id']."bb";
                        array_push($list1, $va);
                    }
                }
            }
            if(isset($data['is_export']) && $data['is_export'] == 1){//如果是导出操作
                return $this->export($list1);
            }
            $count = count($list1);
            return $this->grid_data($list1, $count, $page, $limit);
        }
        return $this->fetch();
    }

    public function export($volist){
        $excel = new Excel();
        $title = [
            'name' => '商品',
            'cate_name' => '商品类别',
            'model' => '属性',
            'unit_name' => '单位',
            'sales_num' => '销售在订量',
            'ck'=>"库存余额",
            'cg_num'=>'采购在订量',
            'low_num'=>'最低库存',
            'jy_num'=>'建议采购量',  
        ]; 
        $arr_list=[];
        if(!empty($volist)){
           $arr_list=$volist;
        }

        //添加标题到数组头部
        array_unshift($arr_list,$title);
        $kong_arr=$title;
        foreach ($kong_arr as $ke1 => $va1) {
            $kong_arr[$ke1]='';
        }
        for ($i=0; $i <3 ; $i++) { 
            array_unshift($arr_list,$kong_arr);
        }
        //根据此参数获取数据中的值，要与表格标题键名对应
        $keys=[];
        foreach ($title as $k2 => $v2) {
            array_push($keys, $k2);
        }

        $merge=[];
        $options=[
            'alignCenter'=>['A','B','C','D','E','F','G','H','I'],//居中
            'bold'=>['A1:I3','A4:I4'],//加粗
            'fontSize'=>['zimu'=>['A1'],'size'=>20],//字体大小
        ];
        $title=[//标题
            'title'=>"存货可用量分析",
            'max_zimu'=>"I",
            'number'=>'3',
        ];
        $excel_export_title="智能补货_" . date('Y-m-d_His') . '.xlsx';//文件名称
        $excel->exportExcel($arr_list,$keys,$excel_export_title,$options,-1,$merge,$title);
        exit();
    }

}
